/*
 * @Author: 孙开源 && sunkaiyuan@namenode.cn
 * @Date: 2023-07-06 15:30:14
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @LastEditTime: 2023-10-23 15:00:16
 * @Description:
 *
 */
import { VXETable } from "vxe-table";
import XEUtils from "xe-utils";

// 自定义全局的格式化处理函数
VXETable.formats.mixin({
	nf: {
		cellFormatMethod({ cellValue }, digits = 2) {
			return cellValue
				? XEUtils.commafy(XEUtils.toNumber(cellValue), { digits })
				: "0.00";
		},
	},
	nf3: {
		cellFormatMethod({ cellValue }, digits = 3) {
			return cellValue
				? XEUtils.commafy(XEUtils.toNumber(cellValue), { digits })
				: "0.000";
		},
	},
	int: {
		cellFormatMethod({ cellValue }) {
			return cellValue
				? parseInt(XEUtils.toNumber(cellValue))
				: "0";
		},
	},
	formatterYesNo: {
		cellFormatMethod({ cellValue }) {

			if (cellValue == 1)
				return '<i class="el-icon el-icon--left" style="font-size: 16px;color:#40b440;"><svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" data-v-ea893728=""><path fill="currentColor" stroke="#40b440" stroke-width="100" d="M77.248 415.04a64 64 0 0 1 90.496 0l226.304 226.304L846.528 188.8a64 64 0 1 1 90.56 90.496l-543.04 543.04-316.8-316.8a64 64 0 0 1 0-90.496z"></path></svg></i>';
			else
				return '<i class="el-icon el-icon--left" style="font-size: 16px;color:red;"><svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" data-v-ea893728=""><path fill="currentColor" stroke="red" stroke-width="100" d="M195.2 195.2a64 64 0 0 1 90.496 0L512 421.504 738.304 195.2a64 64 0 0 1 90.496 90.496L602.496 512 828.8 738.304a64 64 0 0 1-90.496 90.496L512 602.496 285.696 828.8a64 64 0 0 1-90.496-90.496L421.504 512 195.2 285.696a64 64 0 0 1 0-90.496z"></path></svg></i>';
		},
	},
	formatterSample: {
		cellFormatMethod({ cellValue }) {

			if (cellValue == 1)
				return "<span class='el-tag el-tag--danger'>小样</span>";
			else
				return "<span class='el-tag el-tag--success'>大货</span>";
		},
	},

	"color.red": {
		cellFormatMethod({ cellValue }) {
			console.log(cellValue)
			return "<span class='red_b'>" + cellValue + "</span>";
		},
	},
	"color.blue": {
		cellFormatMethod({ cellValue }) {
			return "<span class='blue_b'>" + cellValue + "</span>";
		},
	},
	formatterInspect: {
		cellFormatMethod({ cellValue }) {

			if (cellValue == 1)
				return "<span class='el-tag el-tag--danger'>初检</span>";
			else
				return "<span class='el-tag el-tag--success'>终检</span>";
		},
	},
	formatterYesNoWord: {
		cellFormatMethod({ cellValue }) {
			return cellValue == 1 ? "是" : "否"
		},
	},
	formatterRawIds: {
		cellFormatMethod({ cellValue }) {
			if (cellValue == null) {
				return "";
			}
			var stringvalue = "",
				ss = cellValue.split(","); // 在每个逗号(,)处进行分解。
			if (ss.indexOf("1") != -1) {
				stringvalue += "<span class=el-tag>阳离子</span>";
			}
			if (ss.indexOf("2") != -1) {
				stringvalue += "<span class=el-tag>活性</span>";
			}
			if (ss.indexOf("3") != -1) {
				stringvalue += "<span class=el-tag>分撒</span>";
			}
			if (ss.indexOf("4") != -1) {
				stringvalue += "<span class=el-tag>酸性</span>";
			}
			return stringvalue;
		},
	},
	formatterTag: {
		cellFormatMethod({ cellValue }) {
			if (cellValue == null) {
				return "";
			}
			var tagdata = ['danger', 'success'];
			var stringvalue = "";
			cellValue.map((v, k) => {
				stringvalue += "<span class='el-tag el-tag--" + tagdata[k % 2] + "'>" + (v.label ?? v.name) + "</span>";
			});
			return stringvalue;
		},
	},
	formatterScheduling: {
		cellFormatMethod(data) {
			let cellValue = data.cellValue;
			let permin_width = 19 / 15;
			let now = new Date();

			let baseleft = 39;
			let today = data.column.slots.getcolDate().value;
			let iMin = parseInt((now - today) / 1000 / 60);
			baseleft = iMin * permin_width + baseleft;
			let basehoureLine = "<span style='width:1px;left:" + baseleft + "px' class='spanline'></span>";
			let beging = null

			let string = (cellValue ?? []).map((open_card, index) => {

				if (beging == null) {
					if (open_card.audited_at)
						beging = new Date(open_card.audited_at);
					else
						beging = new Date();
				}
				let iMin = parseInt((beging - now) / 1000 / 60);

				let baseWidth = iMin * permin_width + baseleft;
				let class_name = ['danger', 'warning', 'info', 'success'];
				//15米/min
				let min = open_card.quantity / 15;
				min = min < 60 ? 60 : min;
				//没分钟px宽度
				let width = min * permin_width;
				let string = "<span data-id=" + open_card.main_id + " style='width:" + width + "px;left:" + baseWidth + "px'   class='spandata " + class_name[index % 4] + "'>" + open_card.customer.code + "<br />" + open_card.material.name + " | " + open_card.quantity + " | " + open_card.code + "</span>";
				baseleft += width;

				return string;
			}).join("");
			return basehoureLine + string;
		},
	},

	formatFinancePublic: {
		cellFormatMethod({ cellValue }) {

			let stringvalue = "";
			if (cellValue == '1015' || cellValue == '') {
				stringvalue = "<span class='el-tag el-tag--danger'>私</span>";
			}
			else {
				stringvalue = "<span class='el-tag el-tag--success'>公</span>";
			}
			return stringvalue;
		},
	},
	formatTrack: {
		cellFormatMethod({ cellValue }) {

			let stringvalue = "";
			if (cellValue == '待跟进' || cellValue == '拒绝') {
				stringvalue = "<span class='el-tag el-tag--danger'>" + cellValue + "</span>";
			}
			else if (cellValue == '已跟进') {
				stringvalue = "<span class='el-tag el-tag--warning'>" + cellValue + "</span>";
			}
			else if (cellValue == '暂停') {
				stringvalue = "<span class='el-tag el-tag--info'>" + cellValue + "</span>";
			}
			else {
				stringvalue = "<span class='el-tag el-tag--success'>" + cellValue + "</span>";
			}
			return stringvalue;
		},
	},
	Emergency: {
		cellFormatMethod({ cellValue }) {
			let typeName = "-info";
			switch (cellValue) {
				case "一般":
					typeName = "-success";
					break;
				case "加急":
					typeName = "-warning";
					break;
				case "特急":
					typeName = "-danger";
					break;
			}
			return (
				"<span class='el-tag el-tag-" +
				typeName +
				"'>" +
				cellValue +
				"</span>"
			);
		},
	},
	TrueOrFalse: {
		cellFormatMethod({ cellValue }) {
			let color = "color:";
			let text = "";
			switch (cellValue) {
				case 1:
					color += "blue";
					text = "TURE";
					break;
				case 0:
					color += "red";
					text = "FALSE";
					break;
			}
			return (
				"<span style='font-weight: bold;" +
				color +
				"'>" +
				text +
				"</span>"
			);
		},
	},
	formatterCraftIds: {
		cellFormatMethod({ cellValue }) {
			if (cellValue == null) {
				return "";
			}
			var stringvalue = "",
				ss = cellValue.split(","); // 在每个逗号(,)处进行分解。

			if (ss.indexOf("1") != -1) {
				stringvalue += "<span class=el-tag >酸洗</span>";
			}
			if (ss.indexOf("2") != -1) {
				stringvalue += "<span class=el-tag>皂洗</span>";
			}
			if (ss.indexOf("3") != -1) {
				stringvalue += "<span class=el-tag>固色</span>";
			}
			if (ss.indexOf("4") != -1) {
				stringvalue += "<span class=el-tag>柔软</span>";
			}
			if (ss.indexOf("5") != -1) {
				stringvalue += "<span class=el-tag>还原</span>";
			}
			if (ss.indexOf("6") != -1) {
				stringvalue += "<span class=el-tag>去静电</span>";
			}
			if (ss.indexOf("7") != -1) {
				stringvalue += "<span class=el-tag>防霉</span>";
			}
			return stringvalue;
		},
	},
	formatterColorShadeIds: {
		cellFormatMethod({ cellValue }) {
			if (cellValue == null) {
				return "";
			}
			var stringvalue = "",
				ss = cellValue.split(","); // 在每个逗号(,)处进行分解。

			if (ss.indexOf("1") != -1) {
				stringvalue += "<span class=el-tag >白</span>";
			}
			if (ss.indexOf("2") != -1) {
				stringvalue += "<span class=el-tag>浅</span>";
			}
			if (ss.indexOf("3") != -1) {
				stringvalue += "<span class=el-tag>中</span>";
			}
			if (ss.indexOf("4") != -1) {
				stringvalue += "<span class=el-tag>中深</span>";
			}
			if (ss.indexOf("5") != -1) {
				stringvalue += "<span class=el-tag>深</span>";
			}
			if (ss.indexOf("6") != -1) {
				stringvalue += "<span class=el-tag>黑</span>";
			}
			return stringvalue;
		},
	},
	formatterMaterialCategoryIds: {
		cellFormatMethod({ cellValue }) {
			if (cellValue == null) {
				return "";
			}
			var stringvalue = "",
				ss = cellValue.split(","); // 在每个逗号(,)处进行分解。

			if (ss.indexOf("B") != -1) {
				stringvalue += "<span class=el-tag >包芯纱</span>";
			}
			if (ss.indexOf("Z") != -1) {
				stringvalue += "<span class=el-tag>纤/绵</span>";
			}
			if (ss.indexOf("S") != -1) {
				stringvalue += "<span class=el-tag>三合一</span>";
			}
			if (ss.indexOf("F") != -1) {
				stringvalue += "<span class=el-tag>仿貂</span>";
			}

			return stringvalue;
		},
	},
	formatterColorTwoAudit: {
		cellFormatMethod({ cellValue }) {
			let typeName =
				cellValue != "初审"
					? cellValue === "未审"
						? "-danger"
						: "-success"
					: "-warning";
			return (
				"<span class='el-tag el-tag-" +
				typeName +
				"'>" +
				cellValue +
				"</span>"
			);
		},
	},

	colorFomatter: {
		cellFormatMethod(data) {
			function colorRever(old) {
				var oldv = "0x" + old.toString().replace(/#/g, "");

				var str = "00" + (0xff - oldv).toString(16);
				return str.substring(str.length - 2, str.length);
			}
			function fondcolor(rgb) {
				return (
					colorRever(rgb.substring(0, 2)) +
					colorRever(2, 4) +
					colorRever(4)
				);
			}

			const { cellValue, row } = data;

			let rgb = row.rgb ?? row.color?.rgb ?? "";

			return (
				'<div style="color:#' +
				fondcolor(rgb) +
				";text-align:center;align-items:center;background-color:" +
				rgb +
				';width:100%;height:22px;line-height:22px;">' +
				((cellValue ?? "") == (row.rgb ?? "")
					? "&nbsp;"
					: cellValue ?? "") +
				"</div>"
			);
		},
	},

	formatStatusName: {
		cellFormatMethod({ cellValue }) {
			let typeName =
				cellValue != "已审核" && cellValue != "已入库" && cellValue != "已完结" && cellValue != "启用" && cellValue != "已审" && cellValue != "是" && cellValue != "已检" && cellValue != "收票" && cellValue != "在库"
					? cellValue === "未审核" || cellValue === "未入库" || cellValue === "未完结" || cellValue === "未审" || cellValue === "启用" || cellValue === "否" || cellValue === "未检" || cellValue === "开票" || cellValue === "已抵"
						? "-danger"
						: cellValue === "取消"
							? "-info"
							: "-warning"
					: "-success";
			return (
				"<span class='el-tag el-tag-" +
				typeName +
				"'>" +
				cellValue +
				"</span>"
			);
		},
	},
	formatFinanceStatusName: {
		cellFormatMethod({ cellValue }) {
			let typeName =
				cellValue != "记账"
					? cellValue === "未记"
						? "-danger"
						: cellValue === "作废"
							? "-info"
							: "-warning"
					: "-success";
			return (
				"<span class='el-tag el-tag-" +
				typeName +
				"'>" +
				cellValue +
				"</span>"
			);
		},
	},
	formatSubscribeName: {
		cellFormatMethod({ cellValue }) {
			let typeName =
				cellValue != "已订阅"
					? cellValue === "未订阅"
						? "-danger"
						: "-warning"
					: "-success";
			return (
				"<span class='el-tag el-tag-" +
				typeName +
				"'>" +
				cellValue +
				"</span>"
			);
		},
	},
	StorageOut: {
		cellFormatMethod({ cellValue }) {
			let typeName = cellValue == "待出库" ? "-danger" : "-success";
			return (
				"<span class='el-tag el-tag-" +
				typeName +
				"'>" +
				cellValue +
				"</span>"
			);
		},
	},
	// formatPic({ cellValue, row }) {
	//     return "<span class='el-avatar el-avatar--square'   style='--el-avatar-size:100px;'>" +
	//         "<img src=" + cellValue + " style='object-fit: cover;'>" + row.fileext + "</span>";
	// },
	// 格式化性别
	formatSex: {
		cellFormatMethod({ cellValue }) {
			return cellValue ? (cellValue == 1 ? "男" : "女") : "";
		},
	},
	allowOrNot: {
		cellFormatMethod({ cellValue }) {
			return cellValue == 1 || cellValue == true ? "允许" : "禁止";
		},
	},
	// 格式化下拉选项
	formatSelect: {
		cellFormatMethod({ cellValue }, list) {
			const item = list.find((item) => item.value === cellValue);
			return item ? item.label : "";
		},
	},
	// 格式化日期，默认 yyyy-MM-dd HH:mm:ss
	formatDate: {
		cellFormatMethod({ cellValue }, format) {
			return XEUtils.toDateString(
				cellValue,
				format || "yyyy-MM-dd HH:mm:ss"
			);
		},
	},
	// 四舍五入金额，每隔3位逗号分隔，默认2位数
	formatAmount: {
		cellFormatMethod({ cellValue }, digits = 2) {
			return XEUtils.commafy(XEUtils.toNumber(cellValue), { digits });
		},
	},
	formatDyelot: {
		cellFormatMethod({ cellValue }) {
			cellValue = cellValue ?? "";
			return /^[a-zA-Z]$/.test(cellValue.charAt(0)) ? cellValue.slice(1) : cellValue;
		},
	},
	// 格式化银行卡，默认每4位空格隔开
	formatBankcard: {
		cellFormatMethod({ cellValue }) {
			return XEUtils.commafy(XEUtils.toValueString(cellValue), {
				spaceNumber: 4,
				separator: " ",
			});
		},
	},
	// 四舍五入,默认两位数
	formatFixedNumber: {
		cellFormatMethod({ cellValue }, digits = 2) {
			return XEUtils.toFixed(XEUtils.round(cellValue, digits), digits);
		},
	},
	number: {
		cellFormatMethod({ cellValue }) {
			return XEUtils.toNumber(cellValue);
		},
	},
	// 向下舍入,默认两位数
	formatCutNumber: {
		cellFormatMethod({ cellValue }, digits = 2) {
			return XEUtils.toFixed(XEUtils.floor(cellValue, digits), digits);
		},
	},
	StopTime: {
		cellFormatMethod({ cellValue }) {
			let datetime = Date.parse(new Date(cellValue)) / 1000;
			let datenow = Date.parse(new Date()) / 1000;
			let min = 60;
			let hour = 60 * 60;
			let day = 60 * 60 * 23;
			let datefidd = datenow - datetime;
			if (datefidd < min) {
				return datefidd + "秒";
			}
			if (datefidd > min && datefidd < hour) {
				return Math.floor(datefidd / min) + "分";
			}
			if (datefidd > hour && datefidd < day) {
				return Math.floor(datefidd / hour) + "时";
			}
			if (datefidd > day) {
				return Math.floor(datefidd / day) + "天";
			}
		},
	},
});
